<?php


namespace App\Logic;


use App\Exceptions\ApiException;
use App\Models\OptionModel;
use App\Models\QuestionModel;
use App\Models\SubjectModel;
use App\Models\TeacherModel;
use App\Utils\Singleton;
use Illuminate\Http\Request;
use Illuminate\Http\Response;




class TeacherLogic
{
    use Singleton;

    //获取老师表信息
    public function getTeacher(Request $request,Response $response){
        $name=$request->input('teacher_name');
        $phone=$request->input('phone');
        if (empty($name) || empty($phone)){
            $response->exception = new ApiException(-1,'字段为空');
            return $response;
        }elseif (!empty(TeacherModel::getInstance()->getList(['teacher_name'],['teacher_phone'=>$phone,'teacher_name'=>$name]))){

                return [];
        }else{
            $response->exception = new ApiException(-100,'登录失败');
            return $response;
        }
    }

    //显示科目
    public function getSubject(){

        return SubjectModel::getInstance()->getList(['subject_name','subject_id'],null,'id desc');
    }
    //添加科目
    public function addSubject(Request $request, Response $response){
        $insertData = ['subject_name' => $request->input('subject_name')];
        // dd(SubjectModel::getInstance()->getList('subject_name', $insertData));
        if (!empty(SubjectModel::getInstance()->getOne('subject_name',$insertData))){
            $response->exception = new ApiException(-1,'科目已重复');
        }else{
            $insertData['subject_id']=md5(time());
            SubjectModel::getInstance()->insertData($insertData);
        }
    }
//获取试题????
    public function getQuestion(Request $request,Response $response){
        $type=$request->input('question_type');
        if (empty($type)){
            foreach (['单选题','判断题','多选题','简答题'] as $v) {
                $list[]=QuestionModel::getInstance()->count(['is_Deleted'=>0,'question_type'=>$v,
                    'subject_id'=>$request->input('subject_id')]);
            }
            $number['single_num']=$list[0];
            $number['judge_num']=$list[1];
            $number['more_num']=$list[2];
            $number['subjective_num']=$list[3];
            return [];
        }
        $max=QuestionModel::getInstance()->count(['is_Deleted'=>0,'question_type'=>$type,
            'subject_id'=>$request->input('subject_id')]);
        $page=$request->input('page');
        $num=$request->input('num');
        if (empty($num) || $num<=0){
            $num=10;
        }
        $pagemax=ceil($max/$num);
        if (empty($page) || $page<=1){
            $page=1;
        }elseif ($page>=$pagemax){
            $page=$pagemax;
        }

        foreach (['单选题','判断题','多选题','简答题'] as $v) {
            $list[]=QuestionModel::getInstance()->count(['is_Deleted'=>0,'question_type'=>$v,
                'subject_id'=>$request->input('subject_id')]);
        }
        $number['single_num']=$list[0];
        $number['judge_num']=$list[1];
        $number['more_num']=$list[2];
        $number['subjective_num']=$list[3];

        //var_dump($page,$num);
        //
        return [QuestionModel::getInstance()->getList(QuestionModel::$allColumn,
            ['is_Deleted'=>0,'question_type'=>$type, 'subject_id'=>$request->input('subject_id')],
            'id desc',null,$num,($page-1)*$num),$max,$number];
    }
    public function typeNum(Request $request,Response $response){
        foreach (['单选题','判断题','多选题','简答题'] as $v) {
            $list[]=QuestionModel::getInstance()->count(['is_Deleted'=>0,'question_type'=>$v,
                'subject_id'=>$request->input('subject_id')]);
        }
        $num['single_num']=$list[0];
        $num['judge_num']=$list[1];
        $num['more_num']=$list[2];
        $num['subjective_num']=$list[3];
        return $num;
    }

//添加问题
    public function addQuestion(Request $request,Response $response){
        $insertData = [];
        if (!empty($request->input())){
            foreach (QuestionModel::$allColumn as $k => $v) {
                if(empty($request->input($v)))
                {
                    continue;
                }
                $insertData[$v] = $request->input($v);
            }
            // $insertData=[
            // 'subject_name'=>$request->input('subject_name'),
            // 'subject_id'=>$request->input('subject_id'),
            // //'question_id'=>$request->input('question_id'),
            // 'answer'=>$request->input('answer'),
            // 'question_type'=>$request->input('question_type'),
            // 'question_content'=>$request->input('question_content'),
            // //'is_selected'=>$request->input('is_selected')
            // ];
            if (!empty($insertData)) {
                $insertData['question_id'] = md5(time());
                $options=$request->input('options');
                if (empty(QuestionModel::getInstance()->getList('id',
                    ['question_content'=>$insertData['question_content']]))){
                    QuestionModel::getInstance()->insertData($insertData);
                }else{
                    $response->exception = new ApiException(-1,'题目重复');
                    return $response;
                }
                if (!empty($options)){
                    foreach ($options as $k => $v){
                        $optionData=[];
                        $optionData=[
                            'question_id'=>$insertData['question_id'],
                            'option_id'=>$v['option_id'],
                            'option_content'=>$v['option_content']
                        ];
                        if (!empty($optionData['option_content'])){
                            OptionModel::getInstance()->insertData($optionData);
                        }
                    }
                }

                return [];
            }else{
                $response->exception = new ApiException(-1,'字段为空');
                return $response;
            }
        }else{
            $response->exception = new ApiException(-2,'字段为空');
            return $response;
        }
    }
    //删除问题
    public function delQuestion(Request $request,Response $response){
        if (!empty($request->input('question_id'))){
            QuestionModel::getInstance()->updateData(['is_Deleted'=> 1],
                ['question_id'=>$request->input('question_id')]);
            //getInstance()->updateData(['isDelete' => 1], $updateWhere);
            OptionModel::getInstance()->updateData(['is_Deleted'=> 1],
                ['question_id'=>$request->input('question_id')]);
        }else{
            $response->exception = new ApiException(-1,'字段为空');
            return $response;
        }
    }
    //获取一个问题全部
    public function oneQuestion(Request $request,Response $response){
        if (!empty($request->input('question_id'))){

            $list=objectToArray(QuestionModel::getInstance()->getList(QuestionModel::$allColumn,
                ['is_Deleted'=> 0,'question_id'=>$request->input('question_id')]))[0];
            //getInstance()->updateData(['isDelete' => 1], $updateWhere);

            $data=objectToArray(OptionModel::getInstance()->getList('option_content',['is_Deleted'=> 0,'question_id'=>$request->input('question_id')]));
            $list['options']=$data;
            return $list;
        }else{
            $response->exception = new ApiException(-1,'字段为空');
            return $response;
        }
    }
//编辑问题的保存
    public function editQuestion(Request $request,Response $response){
        if (!empty($request->input())){
            $updateDate=[];
            foreach (QuestionModel::$allColumn as $k => $v) {
                if(empty($request->input($v)) )
                {
                    continue;
                }
                $updateDate[$v] = $request->input($v);
            }
            QuestionModel::getInstance()->updateData($updateDate,['question_id'=>$request->input('question_id')]);
            $aa=$request->input('options');
            if (!empty($aa)) {
                foreach ($aa as $k => $v) {
                    $optionData = [];
                    $question_id = $updateDate['question_id'];
                    $option_id = $v['option_id'];
                    $optionData = [
                        'option_content' => $v['option_content']
                    ];

                    if (!empty($optionData)) {
                        if (!empty(OptionModel::getInstance()->getList('id',
                            ['question_id' => $question_id, 'option_id' => $option_id]))) {
                            OptionModel::getInstance()->updateData($optionData, ['question_id' => $question_id, 'option_id' => $option_id]);
                        } else {
                            $optionData['option_id'] = $option_id;
                            $optionData['question_id'] = $question_id;

                            OptionModel::getInstance()->insertData($optionData);
                        }

                    }
                }
                return [];
            }
        }else{
            $response->exception = new ApiException(-1,'字段为空');
            return $response;
        }
    }




}